// pages/myself/address/editAddress.js
var network = require('../../../utils/network_util.js');
var api = require('../../../utils/api.js');
var Zan = require('../../../dist/index');

Page(Object.assign({}, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    addr: {},
    region: [],
    isSubmit: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "编辑地址",
    })
    if (options.addr) {
      var addr = JSON.parse(options.addr)
      addr.default = addr.isDefault;
      console.log(addr);
      this.setData({
        addr: addr,
        region: [addr.provincName, addr.cityName, addr.districtName],
      })

      isSubmit(this);
    } else {
      this.data.addr.default = 0;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 姓名输入监听
   */
  nameBindKeyInput: function (e) {
    this.data.addr.name = e.detail.value;
    isSubmit(this);
  },

  /**
   * 电话输入监听
   */
  phoneBindKeyInput: function (e) {
    this.data.addr.mobile = e.detail.value;
    isSubmit(this);
  },

  /**
   * 地区选择监听
   */
  bindRegionChange: function (e) {
    this.data.addr.provincName = e.detail.value[0];
    this.data.addr.cityName = e.detail.value[1];
    this.data.addr.districtName = e.detail.value[2];
    this.setData({
      region: e.detail.value
    })
    isSubmit(this);
  },

  /**
   * 地址输入监听
   */
  addressBindKeyInput: function (e) {
    this.data.addr.address = e.detail.value;
    isSubmit(this);
  },

  /**
   * 设置默认值
   */
  switchChange: function (e) {
    if (e.detail.value) {
      this.data.addr.default = 1;
    } else {
      this.data.addr.default = 0;
    }

    this.setData({
      addr: this.data.addr
    })
  },

  /**
   * 提交数据
   */
  formSubmit: function (e) {
    // var addr = {};
    // addr.id = "";
    // addr.name = e.detail.value.name;
    // addr.mobile = e.detail.value.mobile;
    // addr.address = e.detail.value.address;
    // addr.provincName = this.data.region[0];
    // addr.cityName = this.data.region[1];
    // addr.districtName = this.data.region[2];
    // addr.default = this.data.addr.default;
    network._post_json(this, api.modifyAddress, this.data.addr, success);
  },

}))

var isSubmit = function (that) {
  that.setData({
    addr: that.data.addr
  })
  
  var reg = "^((13[0-9])|(14[5|7])|(15([0-9]))|(17([0-9]))|(18[0-9]))\\d{8}$";
  var patrn = new RegExp(reg);
  if (that.data.addr.name && that.data.addr.mobile && that.data.addr.address) {
    if (that.data.addr.name.length > 0 && patrn.exec(that.data.addr.mobile) && that.data.region.length > 0 && that.data.addr.address.replace(/(^\s*)|(\s*$)/g, "").length > 0) {
      that.setData({
        isSubmit: true
      })
    } else {
      that.setData({
        isSubmit: false
      })
    }
  } else {
    that.setData({
      isSubmit: false
    })
  }
}

// 地址修改成功
var success = function (that, res) {
  var pages = getCurrentPages();
  if (pages.length > 1) {
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    if (prePage.refresh && typeof (prePage.refresh) == "function") {
      prePage.refresh();
    }
  }
  wx.navigateBack();
  wx.showToast({
    title: '编辑成功',
  })
}